package com.qingyun.edusysteam.utils;

import com.qingyun.edusysteam.dto.UserDTO;
import org.springframework.stereotype.Component;

/**
 * 基于 ThreadLocal 的用户上下文持有器。
 * 使用：拦截器 preHandle 调用 set()，afterCompletion 调用 clear()。
 */
@Component
public class UserHolder {
    private static final ThreadLocal<UserDTO> CONTEXT = new ThreadLocal<>();

    public void set(UserDTO user) {
        CONTEXT.set(user);
    }

    public UserDTO get() {
        return CONTEXT.get();
    }

    public Long getUserId() {
        UserDTO u = CONTEXT.get();
        return u == null ? null : u.getId();
    }

    public void clear() {
        CONTEXT.remove();
    }
}


